package com.ft.admix.gdt.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;

import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.CSJAdError;
import com.bytedance.sdk.openadsdk.CSJSplashAd;
import com.bytedance.sdk.openadsdk.TTAdConstant;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTAdSdk;
import com.bytedance.sdk.openadsdk.mediation.ad.MediationAdSlot;
import com.ft.admix.ad.MixCustomSplashAdapter;
import com.ft.admix.common.MixConstant;
import com.ft.admix.config.AdConfig;
import com.ft.admix.utils.ThreadUtils;
import com.qq.e.ads.splash.SplashAD;
import com.qq.e.ads.splash.SplashADListener;
import com.qq.e.comm.constants.LoadAdParams;
import com.qq.e.comm.managers.GDTAdSdk;
import com.qq.e.comm.util.AdError;

import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

public class SplashAdapterLoader extends MixCustomSplashAdapter {
    private SplashAD mSplashAd;

    @Override
    protected void loadNetworkAd(Activity context, AdConfig config) {
        if (!Common.isInit) {

        }
        mSplashAd = new SplashAD(context, config.getAdId(), new SplashADListener() {
            @Override
            public void onADDismissed() {
                callSplashAdDismiss();
            }

            @Override
            public void onNoAD(AdError adError) {
                callLoadFailed(adError.getErrorCode(), adError.getErrorMsg());
            }

            @Override
            public void onADPresent() {

            }

            @Override
            public void onADClicked() {
                callSplashAdClicked();
            }

            @Override
            public void onADTick(long l) {

            }

            @Override
            public void onADExposure() {
                callSplashAdShow();
            }

            @Override
            public void onADLoaded(long l) {
                callLoadSuccess();
            }
        });

        mSplashAd.fetchFullScreenAdOnly();
    }

    @Override
    public void show(ViewGroup container) {
        ThreadUtils.runOnUIThreadByThreadPool(new Runnable() {
            @Override
            public void run() {
                if (mSplashAd != null && container != null) {
                    container.removeAllViews();
                    mSplashAd.showAd(container);
                }
            }
        });
    }

    @Override
    public void destroy() {
        mSplashAd = null;
    }

    @Override
    public MixConstant.AdReadyStatus isReadyCondition() {
        /**
         * 在子线程中进行广告是否可用的判断
         */
        Future<MixConstant.AdReadyStatus> future = ThreadUtils.runOnThreadPool(new Callable<MixConstant.AdReadyStatus>() {
            @Override
            public MixConstant.AdReadyStatus call() throws Exception {
                if (mSplashAd != null) {
                    return MixConstant.AdReadyStatus.AD_IS_READY;
                } else {
                    return MixConstant.AdReadyStatus.AD_IS_NOT_READY;
                }
            }
        });
        try {
            MixConstant.AdReadyStatus result = future.get(500, TimeUnit.MILLISECONDS);//设置500毫秒的总超时，避免线程阻塞
            if (result != null) {
                return result;
            } else {
                return MixConstant.AdReadyStatus.AD_IS_NOT_READY;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return MixConstant.AdReadyStatus.AD_IS_NOT_READY;
    }
}
